package com.neulogistics.contrib.procuredcenter.infrastructure.persistence.converter;

import cn.hutool.json.JSONUtil;
import com.neulogistics.contrib.procuredcenter.domain.model.aggregate.Vendor;
import com.neulogistics.contrib.procuredcenter.domain.model.valueobject.Address;
import com.neulogistics.contrib.procuredcenter.infrastructure.persistence.orm.po.VendorPO;
import org.springframework.beans.BeanUtils;

public class VendorEntityPOConverter {
    public static VendorPO entityToPO(Vendor vendor){
        if(vendor == null)
            return null;

        VendorPO vendorPO = new VendorPO();
        // 复制普通属性
        BeanUtils.copyProperties(vendor, vendorPO);
        if(vendor.getVendorAddress() != null){
            // json对象转换
            vendorPO.setVendorAddress(JSONUtil.parseObj(vendor.getVendorAddress()));
        }

        return vendorPO;
    }

    public static Vendor POToEntity(VendorPO vendorPO){
        if(vendorPO == null)
            return null;

        Vendor vendor = new Vendor();
        // 复制普通属性
        BeanUtils.copyProperties(vendorPO, vendor);
        // json对象转换
        vendor.setVendorAddress(JSONUtil.toBean(vendorPO.getVendorAddress(), Address.class));

        return vendor;
    }
}
